<?php
namespace models\repositories;
use Doctrine\ORM\EntityRepository;
use entities;
class CompanyRepository extends EntityRepository {
	public function testIterate(){
		echo "<br>iterasi x";
	}
	
	public function findAll(){
	    return $this->findBy(array(), array('name'=>'asc'));  
	}
	
	public function findAllAsObject(){
		$qb = $this->_em->createQueryBuilder();		
		$qb->select('Company')
		   ->from('models\entities\Company', 'a')
		;
		
		$query = $qb->getQuery();
		return $query->getResult();
	}
	
	public function findAllCompanyJoinInvoiceByRangeDateCurr($startDate, $endDate, $curr){
		$qb = $this->_em->createQueryBuilder();
		$qb->select(array('c', 'i'))
   			->from('models\entities\Company', 'c')
   			->leftJoin('c.invoices', 'i');			
		
		if ($curr>-1){
			$qb->add('where', $qb->expr()->andX(
		       $qb->expr()->andX( 
				    $qb->expr()->lte("i.invoiceDate", ":endDate"),
				    $qb->expr()->gte("i.invoiceDate", ":startDate")
			   ),
			   $qb->expr()->eq('i.currency', ':curr')		
		   	 ))		   	  
		   ->setParameter('startDate', $startDate)
		   ->setParameter('endDate', $endDate)
		   ->setParameter('curr', $curr);
		} else {
			$qb->add('where', 
		       $qb->expr()->andX( 
				    $qb->expr()->lte("i.invoiceDate", ":endDate"),
				    $qb->expr()->gte("i.invoiceDate", ":startDate")
			   )		
		   	 )		   	  
		   ->setParameter('startDate', $startDate)
		   ->setParameter('endDate', $endDate);
		}
			
		$qb->orderBy('c.name','asc');	
		$query = $qb->getQuery();
		return $query->getResult();
	}
	
	public function findAllCompanyJoinInvoiceTaxByRangeDateCurr($startDate, $endDate, $curr){
		$qb = $this->_em->createQueryBuilder();
		$qb->select(array('c', 'i'))
   			->from('models\entities\Company', 'c')
   			->leftJoin('c.invoicesTax', 'i');
		
		if ($curr>-1){
			$qb->add('where', $qb->expr()->andX(
		       $qb->expr()->andX( 
				    $qb->expr()->lte("i.invoiceDate", ":endDate"),
				    $qb->expr()->gte("i.invoiceDate", ":startDate")
			   ),
			   $qb->expr()->eq('i.currency', ':curr')		
		   	 ))		   	  
		   ->setParameter('startDate', $startDate)
		   ->setParameter('endDate', $endDate)
		   ->setParameter('curr', $curr);
		} else {
			$qb->add('where', 
		       $qb->expr()->andX( 
				    $qb->expr()->lte("i.invoiceDate", ":endDate"),
				    $qb->expr()->gte("i.invoiceDate", ":startDate")
			   )		
		   	 )		   	  
		   ->setParameter('startDate', $startDate)
		   ->setParameter('endDate', $endDate);
		}
		$qb->orderBy('c.name','asc');	
		$query = $qb->getQuery();
		return $query->getResult();
	}
	
}

